/*
 *  PClassRegistry.cpp
 *  libavid
 *
 *  Created by Peter on 23.02.12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "PClassRegistry.h"

PClassRegistry::ClassRegistryMap PClassRegistry::_classes;

bool PClassRegistry::isClassRegistered( AObject::PCID pcid )
{
	// Can only return 0 or 1
	return _classes.count( pcid );
}

AObject* PClassRegistry::createInstance( AObject::PCID pcid )
{
	ClassRegistryMap::iterator it = _classes.find( pcid );
	
	// If the class wasn't found in the registry, return NULL
	if( it == _classes.end() )
		return nullptr;
	
	FactoryFunc f = it->second;
	
	TR_CHECK( f != nullptr );
	
	return (*f)();
}

void PClassRegistry::dump( std::ostream& str )
{
	str << "PClassRegistry {" << std::endl;
	str <<   "\t_classes[ " << _classes.size() << " ] : {" << std::endl;
	
	ClassRegistryMap::iterator it = _classes.begin();
	for( ; it != _classes.end(); ++it )
	{
		str << "\t\t";
		writePcidToStream( str, (*it).first );
		str << std::endl;
	}
	
	str <<   "\t}" << std::endl;
	str << "}" << std::endl;
}